var Manager;
var core="";
var server='http://biosual.cbio.uct.ac.za/solr/';
var limitPerQuery=300;


(function ($) {

	$(function () {
		core = (null!=getURLParameter("core") &&"null" != getURLParameter("core") && typeof getURLParameter("core")!="undefined")?getURLParameter("core"):core;
		Manager = new AjaxSolr.Manager({
			solrUrl: server+core
		});
		Manager.limitPerQuery=limitPerQuery;
		Manager.addWidget(new AjaxSolr.GraphWidget({
			id: 'graph',
			target: 'graph'
		}));
		Manager.addWidget(new AjaxSolr.AutocompleteWidget({
			id: 'text',
			target: '#search',
			field: 'text',
			fields: [ 'protein1', 'protein2']
		}));
		Manager.addWidget(new AjaxSolr.CurrentSearchWidget({
			id: 'currentsearch',
			target: '#selection',
		}));
		Manager.addWidget(new AjaxSolr.RulerWidget({
			id: 'ruler',
			target: '#ruler',
		}));
		Manager.addWidget(new AjaxSolr.ContextMenuWidget({
			id: 'context',
			target: 'body',
		}));
		Manager.addWidget(new AjaxSolr.ExpressionWidget({
			id: 'expression',
			target: 'expression',
		}));
		Manager.init();
		Manager.store.addByValue("rows", 300);
		
		//getting  proteins in the URL
		var proteins=getURLParameter("proteins");
		var proteinsA=[];
		if (proteins!=null && proteins!="null" && proteins!=""){
			proteinsA=proteins.split(",");
		}else
			Manager.store.addByValue('q', "*:*");
		var params = {
				facet: true,
				'facet.field': [ 'protein1', 'protein2' ],
				'facet.limit': 20,
				'facet.mincount': 1,
				'f.topics.facet.limit': 50,
				'json.nl': 'map'
		};
		for (var name in params) {
			Manager.store.addByValue(name, params[name]);
		}
		Manager.doRequest();
		Manager.info = new Biojs.DetailsFrame({
		     target: "info",
		     features: {id:"Selection Empty"}
		});
		$("#info").position({
			of: $("#graph"),
		    my: "right bottom",
		    at: "right bottom"
		   });
		for (var i=0;i<proteinsA.length;i++)
			Manager.widgets["currentsearch"].requestInteractionsByProtein(proteinsA[i]);
	});
	function getURLParameter(name) {
	    return decodeURIComponent(
	        (location.search.match(RegExp("[?|&]"+name+'=(.+?)(&|$)'))||[,null])[1]
	    );  
	}
})(jQuery);